package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Switch;


import androidx.appcompat.app.AppCompatActivity;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.utils.MessageUtils;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;

import java.util.List;

public class OutShelfActivity extends AppCompatActivity {

    Button btnWarehouseShelfOff;
    Button btnQMSCheck;
    Button btnFlexShelfOff;
    Button btnInventory;
    Button btnQMSReturn;
    Button btnRawMaterial;
    Button btnSemiFinished;
    Button btnComponent;
    Button btnSparePart;
    Button btnFinishedProduct;

    MiniLoadingDialog mMiniLoadingDialog;

    String transType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_out_shelf);

        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        btnFlexShelfOff = findViewById(R.id.btnScrapShelfOff);
        btnWarehouseShelfOff = findViewById(R.id.btnWarehouseShelfOff);
        btnQMSCheck = (Button) findViewById(R.id.btnQMSCheck);
        btnQMSReturn = (Button) findViewById(R.id.btnQMSReturn);
        btnRawMaterial = (Button) findViewById(R.id.btnRawMaterial);
        btnSemiFinished = (Button) findViewById(R.id.btnSemiFinished);
        btnComponent = (Button) findViewById(R.id.btnComponent);
        btnSparePart = (Button) findViewById(R.id.btnSparePart);
        btnFinishedProduct = (Button) findViewById(R.id.btnFinishedProduct);
        btnInventory=(Button) findViewById(R.id.btnInventory);

        btnFlexShelfOff.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnFlexShelfOff.setEnabled(false);
        btnWarehouseShelfOff.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnWarehouseShelfOff.setEnabled(false);
        btnQMSCheck.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnQMSCheck.setEnabled(false);
        btnQMSReturn.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnQMSReturn.setEnabled(false);
        btnInventory.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnInventory.setEnabled(false);
        initable();


        btnFlexShelfOff.setOnClickListener(v -> {
            transType=btnFlexShelfOff.getText().toString();
            initTrans(btnFlexShelfOff.getText().toString());
        });

        btnWarehouseShelfOff.setOnClickListener(v -> {
            transType=btnWarehouseShelfOff.getText().toString();
            initTrans(btnWarehouseShelfOff.getText().toString());
        });

        btnQMSCheck.setOnClickListener(v -> {
            transType=btnQMSCheck.getText().toString();
            initTrans(btnQMSCheck.getText().toString());
        });

        btnQMSReturn.setOnClickListener(v -> {
            transType=btnQMSReturn.getText().toString();
            initTrans(btnQMSReturn.getText().toString());
        });
        btnInventory.setOnClickListener(v -> {
            transType=btnInventory.getText().toString();
            initTrans(btnInventory.getText().toString());
        });

        btnRawMaterial.setOnClickListener(v -> {
            GetTransTaskData(transType,btnRawMaterial.getText().toString());
        });

        btnSemiFinished.setOnClickListener(v -> {
            GetTransTaskData(transType,btnSemiFinished.getText().toString());
        });

        btnComponent.setOnClickListener(v -> {
            GetTransTaskData(transType,btnComponent.getText().toString());
        });

        btnSparePart.setOnClickListener(v -> {
            GetTransTaskData(transType,btnSparePart.getText().toString());
        });

        btnFinishedProduct.setOnClickListener(v -> {
            GetTransTaskData(transType,btnFinishedProduct.getText().toString());
        });
        initTrans();
    }
    private void initable() {

        btnRawMaterial.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnRawMaterial.setEnabled(false);
        btnSemiFinished.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnSemiFinished.setEnabled(false);
        btnComponent.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnComponent.setEnabled(false);
        btnSparePart.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnSparePart.setEnabled(false);
        btnFinishedProduct.setBackground(getResources().getDrawable(R.drawable.button_bg_disable));
        btnFinishedProduct.setEnabled(false);
    }

    private void initTrans() {
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/GetTransBaseData")
                .syncRequest(false)
                .onMainThread(true)
                .execute(new SimpleCallBack<List<String>>() {
                    @Override
                    public void onSuccess(List<String> response) {
                        mMiniLoadingDialog.dismiss();

                        for (String item : response) {
                            switch (item) {
                                case "仓库下架":
                                    btnFlexShelfOff.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnFlexShelfOff.setEnabled(true);
                                    break;
                                case "投料下架":
                                    btnWarehouseShelfOff.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnWarehouseShelfOff.setEnabled(true);
                                    break;
                                case "QMS抽检":
                                    btnQMSCheck.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnQMSCheck.setEnabled(true);
                                    break;
                                case "QMS归还":
                                    btnQMSReturn.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnQMSReturn.setEnabled(true);
                                    break;
                                case "盘点出库":
                                    btnInventory.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnInventory.setEnabled(true);
                                    break;
                                default:
                                    break;
                            }


                        }

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MessageUtils.errorMsgAndVoice(OutShelfActivity.this, e.getMessage());
                    }
                });
    }

    private void initTrans(String transType) {
        mMiniLoadingDialog.show();
        initable();
        XHttp.get("api/WHSmartWhousePDA/GetMaterialClassData")
                .syncRequest(false)
                .onMainThread(true)
                .params("TransTaskType", transType)
                .execute(new SimpleCallBack<List<String>>() {
                    @Override
                    public void onSuccess(List<String> response) {
                        mMiniLoadingDialog.dismiss();

                        for (String item : response) {
                            switch (item) {
                                case "原材料":
                                    btnRawMaterial.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnRawMaterial.setEnabled(true);
                                    break;
                                case "半成品":
                                    btnSemiFinished.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnSemiFinished.setEnabled(true);
                                    break;
                                case "元器件":
                                    btnComponent.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnComponent.setEnabled(true);
                                    break;
                                case "备件":
                                    btnSparePart.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnSparePart.setEnabled(true);
                                    break;
                                case "次品":
                                    btnFinishedProduct.setBackground(getResources().getDrawable(R.drawable.button_bg));
                                    btnFinishedProduct.setEnabled(true);
                                    break;
                                default:
                                    break;
                            }


                        }

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MessageUtils.errorMsgAndVoice(OutShelfActivity.this, e.getMessage());
                    }
                });
    }

    private void GetTransTaskData(String transType,String MaterialType){
        Intent intent = new Intent(OutShelfActivity.this, OutShelfDetailActivity.class);
        intent.putExtra("transType", transType);
        intent.putExtra("MaterialType", MaterialType);
        startActivityForResult(intent,0);
    }
}